package by.koleda.photoupdater;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import by.koleda.photoupdater.helpers.ImageLoader;
import by.koleda.photoupdater.helpers.ParamCallback;
import by.koleda.photoupdater.manager.ContactsManager;
import by.koleda.photoupdater.manager.ContentManager;
import by.koleda.photoupdater.model.Profile;
import by.koleda.photoupdater.model.VkProfile;

public class ProfileActivity extends Activity implements ParamCallback<List<VkProfile>> {

	private ImageView contactPhoto;
	private TextView displayName;
	private TextView isLinked;
	private EditText vkUid;
	private TextView firstName;
	private TextView lastName;
	private TextView homePhone;
	private TextView mobilePhone;
	
	private Profile profile = null;
	private String uid = null;
	private ProgressDialog mDialog;
	
	public static final String TAG = ProfileActivity.class.getSimpleName();
	
	public static final String EXTRA_PROFILE_MESSAGE = "contactId";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.activity_profile);
		
		contactPhoto = (ImageView) findViewById(R.id.contact_picture);
		displayName = (TextView) findViewById(R.id.contact_display_name);
		isLinked = (TextView) findViewById(R.id.profile_vk_status);
		vkUid = (EditText) findViewById(R.id.profile_vk_uid);
		firstName = (TextView) findViewById(R.id.profile_vk_first_name);
		lastName = (TextView) findViewById(R.id.profile_vk_last_name);
		homePhone = (TextView) findViewById(R.id.profile_vk_home_phone);
		mobilePhone = (TextView) findViewById(R.id.profile_vk_mobile_phone);

		Intent intent = this.getIntent();
		String contactId = intent.getStringExtra(EXTRA_PROFILE_MESSAGE);
		
		profile = ContactsManager.getProfile(this, contactId);
		
		displayName.setText(profile.getDisplayName());
		ImageLoader.get(this).loadContactBitmap(contactPhoto, profile.getContactId());
		if (profile.isVkLinked()) {
			isLinked.setText(R.string.vk_status_linked);
			vkUid.setText("id"+String.valueOf(profile.getVkUid()));
			firstName.setText(profile.getVkFirstName());
			lastName.setText(profile.getVkLastName());
			homePhone.setText(profile.getVkHomePhone());
			mobilePhone.setText(profile.getVkMobilePhone());
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_profile, menu);
		return true;
	}
	
	public void onResyncClick(View view) {
		if (profile == null) {
			return;
		}
		uid = vkUid.getText().toString();
		if (uid == null) {
			return;
		}
		if (uid.contains("id")) {
			uid = uid.substring(2);
		}
		
		if (uid.equals("")) {
			Toast.makeText(ProfileActivity.this, R.string.resync_wrong, Toast.LENGTH_LONG).show();
			return;
		}
		
		showProgressDialog(R.string.loading_title, R.string.resync_accept, false);
		ContentManager.getInstance().getSingleUser(ProfileActivity.this, uid, this);
	}

	@Override
	public void onSuccess(List<VkProfile> profileList) {
		hideProgressDialog();
		if (!profileList.isEmpty()) {
			ContactsManager.addNewVkProfiles(getApplicationContext(), profileList);
			ContactsManager.addCorrectSuggestion(getApplicationContext(), profile.getContactId(), profileList.get(0).getUid().toString());
			ContactsManager.submitSuggestion(getApplicationContext(), profile.getContactId(), uid, profileList.get(0).getPhotoUrl());
			startActivity(this.getIntent());
			Toast.makeText(ProfileActivity.this, R.string.resync_success, Toast.LENGTH_LONG).show();
			Intent intent = new Intent();
			setResult(RESULT_OK, intent);
			finish();
		} else {
			Toast.makeText(ProfileActivity.this, R.string.resync_failure, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onError(Throwable e) {
		hideProgressDialog();
		Toast.makeText(ProfileActivity.this, R.string.resync_failure, Toast.LENGTH_LONG).show();
		Log.e(TAG, e.getMessage());
	}
	
	public void showProgressDialog(int title, int msg, boolean horizontal) {
		CharSequence message = getText(msg);
		mDialog = new ProgressDialog(this);
		mDialog.setTitle(title);
		mDialog.setMessage(message);
		mDialog.setIndeterminate(true);
	    mDialog.setCancelable(false);
	    if (horizontal) {
	    	mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	    }
	    mDialog.show();
	}
	
	public void hideProgressDialog() {
		mDialog.dismiss();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_vk_unlink:
				ContactsManager.unlinkProfile(this, profile);
				startActivity(this.getIntent());
				Toast.makeText(ProfileActivity.this, R.string.unlink_success, Toast.LENGTH_LONG).show();
				finish();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
}
